<!DOCTYPE html>
<html lang="en-us">
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
<meta charset="UTF-8">
<title>How Watcher works | Elasticsearch Guide [7.7] | Elastic</title>
<link rel="home" href="index.html" title="Elasticsearch Guide [7.7]">
<link rel="up" href="xpack-alerting.html" title="Alerting on cluster and index events">
<link rel="prev" href="watcher-getting-started.html" title="Getting started with Watcher">
<link rel="next" href="encrypting-data.html" title="Encrypting sensitive data in Watcher">
<meta name="DC.type" content="Learn/Docs/Elasticsearch/Reference/7.7">
<meta name="DC.subject" content="Elasticsearch">
<meta name="DC.identifier" content="7.7">
<meta name="robots" content="noindex,nofollow">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.optimizely.com/js/18132920325.js"></script>
    <link rel="apple-touch-icon" sizes="57x57" href="/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-icon-180x180.png">
    <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
    <link rel="icon" type="image/png" href="/android-chrome-192x192.png" sizes="192x192">
    <link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96">
    <link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
    <link rel="manifest" href="/manifest.json">
    <meta name="apple-mobile-web-app-title" content="Elastic">
    <meta name="application-name" content="Elastic">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="/mstile-144x144.png">
    <meta name="theme-color" content="#ffffff">
    <meta name="naver-site-verification" content="936882c1853b701b3cef3721758d80535413dbfd">
    <meta name="yandex-verification" content="d8a47e95d0972434">
    <meta name="localized" content="true">
    <meta name="st:robots" content="follow,index">
    <meta property="og:image" content="https://www.elastic.co/static/images/elastic-logo-200.png">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="apple-touch-icon-precomposed" sizes="64x64" href="/favicon_64x64_16bit.png">
    <link rel="apple-touch-icon-precomposed" sizes="32x32" href="/favicon_32x32.png">
    <link rel="apple-touch-icon-precomposed" sizes="16x16" href="/favicon_16x16.png">
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/guide/static/styles.css">
  </head>

  <!--© 2015-2021 Elasticsearch B.V. Copying, publishing and/or distributing without written permission is strictly prohibited.-->

  <body>
    <!-- Google Tag Manager -->
    <script>dataLayer = [];</script><noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-58RLH5" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-58RLH5');</script>
    <!-- End Google Tag Manager -->

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-12395217-16"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-12395217-16');
    </script>

    <!--BEGIN QUALTRICS WEBSITE FEEDBACK SNIPPET-->
    <script type="text/javascript">
      (function(){var g=function(e,h,f,g){
      this.get=function(a){for(var a=a+"=",c=document.cookie.split(";"),b=0,e=c.length;b<e;b++){for(var d=c[b];" "==d.charAt(0);)d=d.substring(1,d.length);if(0==d.indexOf(a))return d.substring(a.length,d.length)}return null};
      this.set=function(a,c){var b="",b=new Date;b.setTime(b.getTime()+6048E5);b="; expires="+b.toGMTString();document.cookie=a+"="+c+b+"; path=/; "};
      this.check=function(){var a=this.get(f);if(a)a=a.split(":");else if(100!=e)"v"==h&&(e=Math.random()>=e/100?0:100),a=[h,e,0],this.set(f,a.join(":"));else return!0;var c=a[1];if(100==c)return!0;switch(a[0]){case "v":return!1;case "r":return c=a[2]%Math.floor(100/c),a[2]++,this.set(f,a.join(":")),!c}return!0};
      this.go=function(){if(this.check()){var a=document.createElement("script");a.type="text/javascript";a.src=g;document.body&&document.body.appendChild(a)}};
      this.start=function(){var a=this;window.addEventListener?window.addEventListener("load",function(){a.go()},!1):window.attachEvent&&window.attachEvent("onload",function(){a.go()})}};
      try{(new g(100,"r","QSI_S_ZN_emkP0oSe9Qrn7kF","https://znemkp0ose9qrn7kf-elastic.siteintercept.qualtrics.com/WRSiteInterceptEngine/?Q_ZID=ZN_emkP0oSe9Qrn7kF")).start()}catch(i){}})();
    </script><div id="ZN_emkP0oSe9Qrn7kF"><!--DO NOT REMOVE-CONTENTS PLACED HERE--></div>
    <!--END WEBSITE FEEDBACK SNIPPET-->

    <div id="elastic-nav" style="display:none;"></div>
    <script src="https://www.elastic.co/elastic-nav.js"></script>

    <!-- Subnav -->
    <div>
      <div>
        <div class="tertiary-nav d-none d-md-block">
          <div class="container">
            <div class="p-t-b-15 d-flex justify-content-between nav-container">
              <div class="breadcrum-wrapper"><span><a href="/guide/" style="font-size: 14px; font-weight: 600; color: #000;">Docs</a></span></div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="main-container">
      <section id="content">
        <div class="content-wrapper">

          <section id="guide" lang="en">
            <div class="container">
              <div class="row">
                <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                  <!-- start body -->
                  <div class="page_header">
<strong>IMPORTANT</strong>: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
<a href="../current/index.html">current release documentation</a>.
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-alerting.html">Alerting on cluster and index events</a></span>
»
<span class="breadcrumb-node">How Watcher works</span>
</div>
<div class="navheader">
<span class="prev">
<a href="watcher-getting-started.html">« Getting started with Watcher</a>
</span>
<span class="next">
<a href="encrypting-data.html">Encrypting sensitive data in Watcher »</a>
</span>
</div>
<div class="chapter xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="how-watcher-works"></a>How Watcher works<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a><a class="xpack_tag" href="/subscriptions"></a>
</h2>
</div></div></div>
<p>You <a class="xref" href="how-watcher-works.html#watch-definition" title="Watch definition">add watches</a> to automatically perform an action when
certain conditions are met. The conditions are generally based on data you’ve
loaded into the watch, also known as the <em>Watch Payload</em>. This payload can be
loaded from different sources - from Elasticsearch, an external HTTP service, or
even a combination of the two.</p>
<p>For example, you could configure a watch to send an email to the sysadmin when a
search in the logs data indicates that there are too many 503 errors in the last
5 minutes.</p>
<p>This topic describes the elements of a watch and how watches operate.</p>
<h3>
<a id="watch-definition"></a>Watch definition<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h3>
<p>A watch consists of a <em>trigger</em>, <em>input</em>, <em>condition</em>, and <em>actions</em>. The actions
define what needs to be done once the condition is met. In addition, you can
define <em>conditions</em> and <em>transforms</em> to process and prepare the watch payload before
executing the actions.</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<a class="xref" href="trigger.html" title="Triggers">Trigger</a>
</span>
</dt>
<dd>
Determines when the watch is checked. A watch must have a trigger.
</dd>
<dt>
<span class="term">
<a class="xref" href="input.html" title="Inputs">Input</a>
</span>
</dt>
<dd>
Loads data into the watch payload. If no input is specified, an empty payload is
loaded.
</dd>
<dt>
<span class="term">
<a class="xref" href="condition.html" title="Conditions">Condition</a>
</span>
</dt>
<dd>
Controls whether the watch actions are executed. If no condition is specified,
the condition defaults to <code class="literal">always</code>.
</dd>
<dt>
<span class="term">
<a class="xref" href="transform.html" title="Payload transforms">Transform</a>
</span>
</dt>
<dd>
Processes the watch payload to prepare it for the watch actions. You can define
transforms at the watch level or define action-specific transforms. Optional.
</dd>
<dt>
<span class="term">
<a class="xref" href="actions.html" title="Actions">Actions</a>
</span>
</dt>
<dd>
Specify what happens when the watch condition is met.
</dd>
</dl>
</div>
<p><a id="watch-definition-example"></a>For example, the following snippet shows a
<a class="xref" href="watcher-api-put-watch.html" title="Put watch API">put watch</a> request that defines a watch that
looks for log error events:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _watcher/watch/log_errors
{
  "metadata" : { <a id="CO507-1"></a><i class="conum" data-value="1"></i>
    "color" : "red"
  },
  "trigger" : { <a id="CO507-2"></a><i class="conum" data-value="2"></i>
    "schedule" : {
      "interval" : "5m"
    }
  },
  "input" : { <a id="CO507-3"></a><i class="conum" data-value="3"></i>
    "search" : {
      "request" : {
        "indices" : "log-events",
        "body" : {
          "size" : 0,
          "query" : { "match" : { "status" : "error" } }
        }
      }
    }
  },
  "condition" : { <a id="CO507-4"></a><i class="conum" data-value="4"></i>
    "compare" : { "ctx.payload.hits.total.value" : { "gt" : 5 }}
  },
  "transform" : { <a id="CO507-5"></a><i class="conum" data-value="5"></i>
    "search" : {
        "request" : {
          "indices" : "log-events",
          "body" : {
            "query" : { "match" : { "status" : "error" } }
          }
        }
    }
  },
  "actions" : { <a id="CO507-6"></a><i class="conum" data-value="6"></i>
    "my_webhook" : {
      "webhook" : {
        "method" : "POST",
        "host" : "mylisteninghost",
        "port" : 9200,
        "path" : "/{{watch_id}}",
        "body" : "Encountered {{ctx.payload.hits.total.value}} errors"
      }
    },
    "email_administrator" : {
      "email" : {
        "to" : "sys.admino@host.domain",
        "subject" : "Encountered {{ctx.payload.hits.total.value}} errors",
        "body" : "Too many error in the system, see attached data",
        "attachments" : {
          "attached_data" : {
            "data" : {
              "format" : "json"
            }
          }
        },
        "priority" : "high"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1279.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO507-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Metadata  - You can attach optional static metadata to a watch.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO507-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>Trigger   - This schedule trigger executes the watch every 5 minutes.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO507-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>Input     - This input searches for errors in the <code class="literal">log-events</code> index and
loads the response into the watch payload.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO507-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>Condition - This condition checks to see if there are more than 5 error
events (hits in the search response). If there are, execution
continues for all <code class="literal">actions</code>.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO507-5"><i class="conum" data-value="5"></i></a></p>
</td>
<td align="left" valign="top">
<p>Transform - If the watch condition is met, this transform loads all of the
errors into the watch payload by searching for the errors using
the default search type, <code class="literal">query_then_fetch</code>. All of the watch
actions have access to this payload.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO507-6"><i class="conum" data-value="6"></i></a></p>
</td>
<td align="left" valign="top">
<p>Actions   - This watch has two actions. The <code class="literal">my_webhook</code> action notifies a
3rd party system about the problem. The <code class="literal">email_administrator</code>
action sends a high priority email to the system administrator.
The watch payload that contains the errors is attached to the
email.</p>
</td>
</tr>
</table>
</div>
<h3>
<a id="watch-execution"></a>Watch execution<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h3>
<p><a id="schedule-scheduler"></a>When you add a watch, Watcher immediately registers its trigger with the
appropriate trigger engine. Watches that have a <code class="literal">schedule</code> trigger are
registered with the <code class="literal">scheduler</code> trigger engine.</p>
<p>The scheduler tracks time and triggers watches according to their schedules.
On each node, that contains one of the <code class="literal">.watches</code> shards, a scheduler, that is
bound to the watcher lifecycle runs. Even though all primaries and replicas are
taken into account, when a watch is triggered, watcher also ensures, that each
watch is only triggered on one of those shards. The more replica shards you
add, the more distributed the watches can be executed. If you add or remove
replicas, all watches need to be reloaded.  If a shard is relocated, the
primary and all replicas of this particular shard will reload.</p>
<p>Because the watches are executed on the node, where the watch shards are, you can create
dedicated watcher nodes by using shard allocation filtering.</p>
<p>You could configure nodes with a dedicated <code class="literal">node.attr.role: watcher</code> property and
then configure the <code class="literal">.watches</code> index like this:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT .watches/_settings
{
  "index.routing.allocation.include.role": "watcher"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1280.console"></div>
<p>When the Watcher service is stopped, the scheduler stops with it. Trigger
engines use a separate thread pool from the one used to execute watches.</p>
<p>When a watch is triggered, Watcher queues it up for execution. A <code class="literal">watch_record</code>
document is created and added to the watch history and the watch’s status is set
to <code class="literal">awaits_execution</code>.</p>
<p>When execution starts, Watcher creates a watch execution context for the watch.
The execution context provides scripts and templates with access to the watch
metadata, payload, watch ID, execution time, and trigger information. For more
information, see <a class="xref" href="how-watcher-works.html#watch-execution-context" title="Watch execution context">Watch Execution Context</a>.</p>
<p>During the execution process, Watcher:</p>
<div class="olist orderedlist">
<ol class="orderedlist">
<li class="listitem">
Loads the input data as the payload in the watch execution context. This makes
the data available to all subsequent steps in the execution process. This step
is controlled by the input of the watch.
</li>
<li class="listitem">
Evaluates the watch condition to determine whether or not to continue processing
the watch. If the condition is met (evaluates to <code class="literal">true</code>), processing advances
to the next step. If it is not met (evaluates to <code class="literal">false</code>), execution of the watch
stops.
</li>
<li class="listitem">
Applies transforms to the watch payload (if needed).
</li>
<li class="listitem">
Executes the watch actions granted the condition is met and the watch is not
<a class="xref" href="how-watcher-works.html#watch-acknowledgment-throttling" title="Watch acknowledgment and throttling">throttled</a>.
</li>
</ol>
</div>
<p>When the watch execution finishes, the execution result is recorded as a
<em>Watch Record</em> in the watch history. The watch record includes the execution
time and duration, whether the watch condition was met, and the status of each
action that was executed.</p>
<p>The following diagram shows the watch execution process:</p>
<div class="imageblock text-center">
<div class="content">
<img src="images/watch-execution.jpg" alt="watch execution">
</div>
</div>
<h3>
<a id="watch-acknowledgment-throttling"></a>Watch acknowledgment and throttling<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h3>
<p>Watcher supports both time-based and acknowledgment-based throttling. This
enables you to prevent actions from being repeatedly executed for the same event.</p>
<p>By default, Watcher uses time-based throttling with a throttle period of 5
seconds. This means that if a watch is executed every second, its actions are
performed a maximum of once every 5 seconds, even when the condition is always
met. You can configure the throttle period on a per-action basis or at the
watch level.</p>
<p>Acknowledgment-based throttling enables you to tell Watcher not to send any more
notifications about a watch as long as its condition is met. Once the condition
evaluates to <code class="literal">false</code>, the acknowledgment is cleared and Watcher resumes executing
the watch actions normally.</p>
<p>For more information, see <a class="xref" href="actions.html#actions-ack-throttle" title="Acknowledgement and throttling">Acknowledgement and throttling</a>.</p>
<h3>
<a id="watch-active-state"></a>Watch active state<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h3>
<p>By default, when you add a watch it is immediately set to the <em>active</em> state,
registered with the appropriate trigger engine, and executed according
to its configured trigger.</p>
<p>You can also set a watch to the <em>inactive</em> state. Inactive watches are not
registered with a trigger engine and can never be triggered.</p>
<p>To set a watch to the inactive state when you create it, set the
<a class="xref" href="watcher-api-put-watch.html" title="Put watch API"><code class="literal">active</code></a> parameter to <em>inactive</em>. To
deactivate an existing watch, use the
<a class="xref" href="watcher-api-deactivate-watch.html" title="Deactivate watch API">deactivate watch API</a>. To reactivate an
inactive watch, use the
<a class="xref" href="watcher-api-activate-watch.html" title="Activate watch API">activate watch API</a>.</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>You can use the <a class="xref" href="watcher-api-execute-watch.html" title="Execute watch API">execute watch API</a>
to force the execution of a watch even when it is inactive.</p>
</div>
</div>
<p>Deactivating watches is useful in a variety of situations. For example, if you
have a watch that monitors an external system and you need to take that system
down for maintenance, you can deactivate the watch to prevent it from falsely
reporting availability issues during the maintenance window.</p>
<p>Deactivating a watch also enables you to keep it around for future use without
deleting it from the system.</p>
<h3>
<a id="scripts-templates"></a>Scripts and templates<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h3>
<p>You can use scripts and templates when defining a watch. Scripts and templates
can reference elements in the watch execution context, including the watch payload.
The execution context defines variables you can use in a script and parameter
placeholders in a template.</p>
<p>Watcher uses the Elasticsearch script infrastructure, which supports
<a class="xref" href="how-watcher-works.html#inline-templates-scripts" title="Inline templates and scripts">inline</a> and <a class="xref" href="how-watcher-works.html#stored-templates-scripts" title="Stored templates and scripts">stored</a>.
Scripts and templates are compiled
and cached by Elasticsearch to optimize recurring execution. Autoloading is also
supported. For more information, see <a class="xref" href="modules-scripting.html" title="Scripting">Scripting</a> and
<a class="xref" href="modules-scripting-using.html" title="How to use scripts"><em>How to use scripts</em></a>.</p>
<h4>
<a id="watch-execution-context"></a>Watch execution context<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h4>
<p>The following snippet shows the basic structure of the <em>Watch Execution Context</em>:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "ctx" : {
    "metadata" : { ... }, <a id="CO508-1"></a><i class="conum" data-value="1"></i>
    "payload" : { ... }, <a id="CO508-2"></a><i class="conum" data-value="2"></i>
    "watch_id" : "&lt;id&gt;", <a id="CO508-3"></a><i class="conum" data-value="3"></i>
    "execution_time" : "20150220T00:00:10Z", <a id="CO508-4"></a><i class="conum" data-value="4"></i>
    "trigger" : { <a id="CO508-5"></a><i class="conum" data-value="5"></i>
      "triggered_time" : "20150220T00:00:10Z",
      "scheduled_time" : "20150220T00:00:00Z"
    },
    "vars" : { ... } <a id="CO508-6"></a><i class="conum" data-value="6"></i>
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO508-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Any static metadata specified in the watch definition.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO508-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>The current watch payload.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO508-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>The id of the executing watch.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO508-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>A timestamp that shows when the watch execution started.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO508-5"><i class="conum" data-value="5"></i></a></p>
</td>
<td align="left" valign="top">
<p>Information about the trigger event. For a <code class="literal">schedule</code> trigger, this
consists of the <code class="literal">triggered_time</code> (when the watch was triggered)
and the <code class="literal">scheduled_time</code> (when the watch was scheduled to be triggered).</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO508-6"><i class="conum" data-value="6"></i></a></p>
</td>
<td align="left" valign="top">
<p>Dynamic variables that can be set and accessed by different constructs
during the execution. These variables are scoped to a single execution
(i.e they’re not persisted and can’t be used between different executions
of the same watch)</p>
</td>
</tr>
</table>
</div>
<h4>
<a id="scripts"></a>Using scripts<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h4>
<p>You can use scripts to define <a class="xref" href="condition-script.html" title="Script condition">conditions</a> and
<a class="xref" href="transform-script.html" title="Script payload transform">transforms</a>. The default scripting language is
<a class="xref" href="modules-scripting-painless.html" title="Painless scripting language">Painless</a>.</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>Starting with 5.0, Elasticsearch is shipped with the new
      <a class="xref" href="modules-scripting-painless.html" title="Painless scripting language">Painless</a> scripting language.
      Painless was created and designed specifically for use in Elasticsearch.
      Beyond providing an extensive feature set, its biggest trait is that it’s
      properly sandboxed and safe to use anywhere in the system (including in
      Watcher) without the need to enable dynamic scripting.</p>
</div>
</div>
<p>Scripts can reference any of the values in the watch execution context or values
explicitly passed through script parameters.</p>
<p>For example, if the watch metadata contains a <code class="literal">color</code> field
(e.g. <code class="literal">"metadata" : {"color": "red"}</code>), you can access its value with the via the
<code class="literal">ctx.metadata.color</code> variable. If you pass in a <code class="literal">color</code>  parameter as part of the
condition or transform definition (e.g. <code class="literal">"params" : {"color": "red"}</code>), you can
access its value via the <code class="literal">color</code> variable.</p>
<h4>
<a id="templates"></a>Using templates<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h4>
<p>You use templates to define dynamic content for a watch. At execution time,
templates pull in data from the watch execution context. For example, you can use
a template to populate the <code class="literal">subject</code> field for an <code class="literal">email</code> action with data stored
in the watch payload. Templates can also access values explicitly passed through
template parameters.</p>
<p>You specify templates using the <a href="https://mustache.github.io" class="ulink" target="_top">Mustache</a> scripting
language.</p>
<p>For example, the following snippet shows how templates enable dynamic subjects
in sent emails:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "actions" : {
    "email_notification" : {
      "email" : {
        "subject" : "{{ctx.metadata.color}} alert"
      }
    }
  }
}</pre>
</div>
<h5>
<a id="inline-templates-scripts"></a>Inline templates and scripts<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h5>
<p>To define an inline template or script, you simply specify it directly in the
value of a field. For example, the following snippet configures the subject of
the <code class="literal">email</code> action using an inline template that references the <code class="literal">color</code> value in
the context metadata.</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"actions" : {
  "email_notification" : {
     "email" : {
       "subject" : "{{ctx.metadata.color}} alert"
     }
   }
  }
}</pre>
</div>
<p>For a script, you simply specify the inline script as the value of the <code class="literal">script</code>
field. For example:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"condition" : {
  "script" : "return true"
}</pre>
</div>
<p>You can also explicitly specify the inline type by using a formal object
definition as the field value. For example:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"actions" : {
  "email_notification" : {
    "email" : {
      "subject" : {
         "source" : "{{ctx.metadata.color}} alert"
      }
    }
  }
}</pre>
</div>
<p>The formal object definition for a script would be:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"condition" : {
  "script" : {
    "source": "return true"
  }
}</pre>
</div>
<h5>
<a id="stored-templates-scripts"></a>Stored templates and scripts<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/how-watcher-works.asciidoc">edit</a>
</h5>
<p>If you <a class="xref" href="modules-scripting-using.html#modules-scripting-stored-scripts" title="Stored scripts">store</a>
your templates and scripts, you can reference them by id.</p>
<p>To reference a stored script or template, you use the formal object definition
and specify its id in the <code class="literal">id</code> field. For example, the following snippet
references the <code class="literal">email_notification_subject</code> template:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  ...
  "actions" : {
    "email_notification" : {
      "email" : {
        "subject" : {
          "id" : "email_notification_subject",
          "params" : {
            "color" : "red"
          }
        }
      }
    }
  }
}</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="watcher-getting-started.html">« Getting started with Watcher</a>
</span>
<span class="next">
<a href="encrypting-data.html">Encrypting sensitive data in Watcher »</a>
</span>
</div>
</div>

                  <!-- end body -->
                </div>
                <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                  <div id="rtpcontainer" style="display: block;">
                    <div class="mktg-promo">
                      <h3>Most Popular</h3>
                      <ul class="icons">
                        <li class="icon-elasticsearch-white"><a href="https://www.elastic.co/webinars/getting-started-elasticsearch?baymax=default&amp;elektra=docs&amp;storm=top-video">Get Started with Elasticsearch: Video</a></li>
                        <li class="icon-kibana-white"><a href="https://www.elastic.co/webinars/getting-started-kibana?baymax=default&amp;elektra=docs&amp;storm=top-video">Intro to Kibana: Video</a></li>
                        <li class="icon-logstash-white"><a href="https://www.elastic.co/webinars/introduction-elk-stack?baymax=default&amp;elektra=docs&amp;storm=top-video">ELK for Logs &amp; Metrics: Video</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </section>

        </div>


<div id="elastic-footer"></div>
<script src="https://www.elastic.co/elastic-footer.js"></script>
<!-- Footer Section end-->

      </section>
    </div>

<script src="/guide/static/jquery.js"></script>
<script type="text/javascript" src="/guide/static/docs.js"></script>
<script type="text/javascript">
  window.initial_state = {}</script>
  </body>
</html>
